/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_CPP_RTPS_TRANSPORT_UDSSTREAMSENDERRESOURCE_HPP_
#define SRC_CPP_RTPS_TRANSPORT_UDSSTREAMSENDERRESOURCE_HPP_

#include <deps/common/LocatorsIterator.hpp>
#include <transport/SenderResource.h>

#include "UDSStreamTransportInterface.h"
#include "UDSStreamChannelResource.h"

namespace vbs {
namespace transport {
using SenderResource = vbs::transport::SenderResource;
class UDSStreamSenderResource : public SenderResource {
 public:
    UDSStreamSenderResource(UDSStreamTransportInterface& transport, std::shared_ptr<UDSStreamChannelResource>& channel)
        : SenderResource(transport.kind()), channel_(channel) {
        clean_up = [this, &transport]() {
            transport.CloseOutputChannel(channel_);
        };

        send_lambda_ = [this, &transport](ertps::rtps::octet* data, uint32_t dataSize,
                                          ertps::rtps::LocatorsIterator* destination_locators_begin,
                                          ertps::rtps::LocatorsIterator* destination_locators_end) -> bool {
            return transport.send(data, dataSize, channel_, destination_locators_begin, destination_locators_end);
        };
        async_send_lambda_ = [this, &transport](ertps::rtps::octet* data, uint32_t dataSize,
                                                ertps::rtps::LocatorsIterator* destination_locators_begin,
                                                ertps::rtps::LocatorsIterator* destination_locators_end,
                                                AsyncSendHandler handler) -> bool {
            return transport.async_send(data, dataSize, channel_, destination_locators_begin, destination_locators_end,
                                        std::move(handler));
        };
    }

    virtual ~UDSStreamSenderResource() {
        if (clean_up) {
            clean_up();
        }
    }

    std::shared_ptr<UDSStreamChannelResource> channel() { return channel_; }

    static UDSStreamSenderResource* cast(TransportInterface& transport, SenderResource* sender_resource) {
        UDSStreamSenderResource* return_resource = nullptr;

        if (sender_resource->kind() == transport.kind()) {
            return_resource = dynamic_cast<UDSStreamSenderResource*>(sender_resource);
        }

        return return_resource;
    }

 private:
    UDSStreamSenderResource() = delete;

    UDSStreamSenderResource(const SenderResource&) = delete;

    UDSStreamSenderResource& operator=(const SenderResource&) = delete;

    std::shared_ptr<UDSStreamChannelResource> channel_;
};

}  // namespace transport
}  // namespace vbs

#endif
